//暴露mixins对象
import clipboard from './clipboard'
import {CHANGE_BIT_FLAG, ifNullMergeFromObject, isNullOrUndefined, mergeFromObject, safeGet} from "./core";

export const sksDialogFormMixin = {
    data() {
        return {
            allLoading: false,
            // //是否为超管
            // isAdmin: false,
            // //是否为管理员
            // isSystem: false,

            showDialog: false,
            mainDialogInfo: {
                uid: undefined,
                //true是否已加载完成
                title: undefined,
                width: undefined,
                selectLabel: undefined,
                selectRowClick: undefined,
                showQueryForm: undefined,

                //主键
                uniqueId: undefined,
                //true: 不可编辑
                readonly: false,
                submitCallback: undefined,
                //提交成功,返回后端传回的信息
                submitCallbackSuccess: undefined,

                //submitCallbackSuccess:({selectRow})=>{}
                //选择对话框时,选择列表行后触发
                selectCallback: undefined,
            },
            defaultMainDialogInfo: {
                //对话框ID
                uid: undefined,
                title: '请选择',
                width: '500px',
                selectLabel: '选择',
                selectCallback: undefined,
                selectRowClick: true,
                showQueryForm: false,

                //主键
                uniqueId: undefined,
                //true: 不可编辑
                readonly: false,
                //编辑数据对话框使用
                //推荐返回数据格式
                //submitCallback:({submitData})=>{}
                submitCallback: undefined,
            },
            form: {},
            defaultForm: {},
            defaultFormNotNull: {},

            //页面传得额外对象,重新调用 show() 时会显示清空
            extra: {},
            defaultExtra: {},
            defaultExtraNotNull: {},

            title: '标题',

            //字典数据都放这里
            dictDataMap: new Map(),
            dictDataMapLoading: false,
            //用于选择关联表的信息,自己在页面中重新定义
            //该类中注释掉,否则会出现 undefined
            // sksSelectMap: {},

            rules: {},

            //取消按钮点击时,关闭对话框也调用 this.$emit('close')
            cancelClickEmitCloseFunc: false,
            //1: 确定按下
            //2: 取消按下
            buttonClickType: 0,
        }
    },
    computed:{
        changeBitFlagIsNone(){
            if(isNullOrUndefined(this.form)){
                return true;
            }
            if(isNullOrUndefined(this.form.virtualChangeBitFlag)){
                return true;
            }
            return this.form.virtualChangeBitFlag==CHANGE_BIT_FLAG.none
        },
        changeBitFlagToBeModify(){
            if(isNullOrUndefined(this.form)){
                return false;
            }
            if(isNullOrUndefined(this.form.virtualChangeBitFlag)){
                return false;
            }
            return this.form.virtualChangeBitFlag==CHANGE_BIT_FLAG.modify
        },
        changeBitFlagToBeAdd(){
            if(isNullOrUndefined(this.form)){
                return false;
            }
            if(isNullOrUndefined(this.form.virtualChangeBitFlag)){
                return false;
            }
            return this.form.virtualChangeBitFlag==CHANGE_BIT_FLAG.add
        },
    },
    beforeCreate() {
        // console.log("混入的beforeCreated");

    },
    created() {
        console.log('sksDialogForm.created')
        // console.log("sks-form.created.this",this)
        // console.log("sks-form.created.mainDialogInfo",this.mainDialogInfo)
        // console.log("sks-form.created.defaultMainDialogInfo",this.defaultMainDialogInfo)
        mergeFromObject(this.mainDialogInfo, this.defaultMainDialogInfo, this.initMainDialogInfo())
        // console.log("sks-form.created")
        this.sksFormInit()
    },
    mounted() {
        // console.log("sks-form.mounted")
        this.sksFormInit()
    },
    methods: {
        /**
         * 初始化 mainDialogInfo 的一些值
         * @return {}  例如: {title: '新建'} 显示工具栏的搜索放大镜
         */
        //这里不允许写任何代码,有需求请在组建中重新
        initMainDialogInfo() {
            this.initMainDialogInfoBefore()
            return {}
        },
        //initMainDialogInfo 执行前
        //这里不允许写任何代码,有需求请在组建中重新
        initMainDialogInfoBefore() {

        },
        /**
         * 需要获取的字典
         * @return []
         [
         {
                        prop: 'processStatus',
                        dictType: 'status',
                       //字典的数据类型,如果为 Number 会将字典数据转换为数字类型
                        dictValueType: 'Number'
                      }
         ]
         */
        //这里不允许写任何代码,有需求请在组建中重新
        initDictTypeConf() {
            return []
        },
        show({ extra,form, dialogInfo } = {}) {
            this.buttonClickType=0
            this.showBefore()
            // this.wblog('form',form)('sks.show.dialogInfo', dialogInfo)('this.mainDialogInfo', this.mainDialogInfo)('this.defaultMainDialogInfo', this.defaultMainDialogInfo)
            mergeFromObject(this.mainDialogInfo, this.defaultMainDialogInfo, this.initMainDialogInfo(), dialogInfo)
            this.extra=mergeFromObject({},this.defaultExtra,extra)
            ifNullMergeFromObject(this.extra, this.defaultExtraNotNull)
            this.showDialog = true
            this.resetMainForm(form)
            this.showAfter()
        },
        //这里不允许写任何代码,有需求请在组建中重新
        showBefore() {

        },
        //这里不允许写任何代码,有需求请在组建中重新
        showAfter() {
        },
        sksFormInit({ form } = {}) {
            this.resetMainForm(form)
        },
        resetMainForm(data) {
            let form = mergeFromObject({}, this.defaultForm, data)
            ifNullMergeFromObject(form, this.defaultFormNotNull)
            form = this.resetMainFormBefore(form)

            this.form = form
            this.resetForm('form')
            // console.log("sks-form.this.form=",this.form)
        },
        //全局只读
        globalReadonly(rowIn, fieldName) {
            if (this.mainDialogInfo.readonly) {
                return true
            }
            let row = rowIn !== undefined ? rowIn : this.form
            if (row.status != undefined && row.status == 1) {
                return true
            }
            return false
        },
        //form 重新赋值前处理
        //这里不允许写任何代码,有需求请在组建中重新
        resetMainFormBefore(form) {
            return form
        },
        submitMainDialog({ args = {} }={}) {
            this.submitMainDialogBefore()
            this.$refs.form?.validate(valid => {
                console.log('form.validate.valid', valid)
                if (!valid) {
                    return
                }
                this.validateSuccess({ args: args })
            })
            this.buttonClickType=1
        },
        //进行校验前执行,子表校验等
        submitMainDialogBefore() {

        },
        //校验通过后执行
        //这里不允许写任何代码,有需求请在组建中重新
        validateSuccess({ args: args = {} }) {
            //比如成功后跳转
            const { successJump } = args
        },
        cancelMainDialog() {
            this.showDialog = false
            this.buttonClickType=2
        },
        closeDialogBefore(){

        },
        closeDialog() {
            this.closeDialogBefore()
            console.log("closeDialog")
            //关闭对话框时会自动隐藏显示上传信息进度 start
            for (let key of Object.keys(this.$refs)) {
                if(this.$refs[key]){
                    console.log("closeDialog.key",key,safeGet(this.$refs,`${key}.$options.name`))
                    //如果存在附件组建,则隐藏上传信息
                    if(safeGet(this.$refs,`${key}.$options.name`)=='DocumentManage'){
                        this.$refs[key]?.hideUploadInfo()
                    }
                }
            }
            //关闭对话框时会自动隐藏显示上传信息进度 end
            this.showDialog = false
            if(this.buttonClickType==1){
                this.$emit('close')
            }else if(this.buttonClickType==2){
                if(this.cancelClickEmitCloseFunc){
                    this.$emit('close')
                }
            }
        },
        clearFormValidate(propName){
            this.$refs?.form?.clearValidate(propName)
        },

        toBeAdd(){
            return this.form.virtualChangeBitFlag == CHANGE_BIT_FLAG.add
        },
        toBeModify(){
            return  this.form.virtualChangeBitFlag==CHANGE_BIT_FLAG.modify
        },
        handleClipboard(text, event) {
            clipboard(text, event)
        }
    }
}
